<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible = true">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType">
          <template #default="scope">
            {{ formatChargeType(scope.row.chargeType ) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editRule(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delRule(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top:10px;float:right">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="currentChange" />
    </div>
    <add-rule :dialogVisible.sync="dialogVisible" ref="addRule" />
  </div>
</template>

<script>
import { getRuleListAPI } from '@/api/car'
import { deleteRuleAPI } from '@/api/rule'
import { utils, writeFileXLSX } from 'xlsx'
import AddRule from './components/AddRule.vue'
export default {
  name: 'Building',
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false
    }
  },
  components: {
    AddRule
  },
  methods: {
    async getRuleList() {
      const { data } = await getRuleListAPI(this.params)
      this.ruleList = data.rows
      this.total = data.total
    },
    currentChange(page) {
      this.params.page = page
      this.getRuleList()
    },
    indexMethod(row) {
      return (this.params.page - 1) * this.params.pageSize + row + 1
    },
    exportToExcel() {
      /*
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表，要求一个对象数组格式
      const worksheet = utils.json_to_sheet([
        { name: '张三', age: 18 },
        { name: '李四', age: 20 }
      ])
      // 把工作表添加到工作簿 Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [['姓名', '年龄']], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAo0.xlsx')
      */

      const workbook = utils.book_new()
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 过滤掉不需要的数据字段
      const sheetData = this.ruleList.map(item => {
        const obj = {}
        for (const key of tableHeaderKeys) {
          obj[key] = item[key]
        }
        return obj
      })
      const worksheet = utils.json_to_sheet(sheetData)
      utils.book_append_sheet(workbook, worksheet, 'Data')
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      writeFileXLSX(workbook, 'SheetJSVueAo0.xlsx')
    },
    formatChargeType(type) {
      const TYPEMAP = {
        'duration': '按时长收费',
        'turn': '按次收费',
        'partition': '分段收费'
      }
      return TYPEMAP[type]
    },
    async delRule(id) {
      try {
        await this.$confirm('您确定要删除该规则吗？', '温馨提示')
        await deleteRuleAPI(id)
        this.$message.success('删除成功')
        this.params.page = 1
        this.getRuleList()
      } catch (e) {
        console.log(e)
      }
    },
    async editRule(id) {
      this.dialogVisible = true
      this.$refs.addRule.getParkingRule(id)
    }
  },
  created() {
    this.getRuleList()
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
